/*
 * Dijjer - A Peer to Peer HTTP Cache
 * Copyright (C) 2004,2005 Change.Tv, Inc
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package dijjer.io.xfer;

import java.util.HashMap;
import java.util.Map;
import dijjer.Dijjer;
import dijjer.io.comm.AbstractPeer;
import dijjer.io.comm.Peer;
import dijjer.io.comm.WebPeer;
import dijjer.util.logging.Logger;

public class PacketThrottle {

	protected static final float PACKET_DROP_DECREASE_MULTIPLE = 0.875f;
	protected static final float PACKET_TRANSMIT_INCREMENT = (4 * (1 - (PACKET_DROP_DECREASE_MULTIPLE * PACKET_DROP_DECREASE_MULTIPLE))) / 3;
	public static final float MAX_DELAY = 1000;
	protected static float minDelay = 100;
	public static final String VERSION = "$Id: PacketThrottle.java 34 2009-03-20 13:27:26Z mathias.demare $";
	public static final float DEFAULT_DELAY = 200;
	private static Map _throttles = new HashMap();
	private static Map _lastThrottleUse = new HashMap();
	private final AbstractPeer _peer;
	private float _roundTripTime = 500, _totalPackets = 0, _droppedPackets = 0;
	private float _simulatedWindowSize = 2f;

	public static PacketThrottle getThrottle(AbstractPeer receiver) {
		if (!_throttles.containsKey(receiver)) {
			_throttles.put(receiver, new PacketThrottle(receiver));
		}
		PacketThrottle pt = (PacketThrottle) _throttles.get(receiver);
		return pt;
	}

	public static void setMaxUploadBandwidth(int maxUpload) {
		minDelay = 1024 / maxUpload;
	}
	
	private PacketThrottle(AbstractPeer peer) {
		_peer = peer;
	}

	public void setRoundTripTime(long rtt) {
		_roundTripTime = Math.max(rtt, 10);
	}

	public void notifyOfPacketSent() {
		_simulatedWindowSize += PACKET_TRANSMIT_INCREMENT;
		_totalPackets++;
	}

	public void notifyOfPacketLoss(int numLost) {
		_droppedPackets += numLost;
		_simulatedWindowSize -= (PACKET_TRANSMIT_INCREMENT * numLost);
		_simulatedWindowSize *= Math.pow(PACKET_DROP_DECREASE_MULTIPLE, numLost);
	}

	public long getDelay() {
		float winSizeForMinPacketDelay = (_roundTripTime / minDelay);
		if (_simulatedWindowSize > winSizeForMinPacketDelay) {
			_simulatedWindowSize = winSizeForMinPacketDelay;
		}
		if (_simulatedWindowSize < 1) {
			_simulatedWindowSize = 1;
		}
		return (long)Math.min(Math.max(minDelay, (long) ((float) _roundTripTime / _simulatedWindowSize)), MAX_DELAY);
	}

	public String toString() {
		long delay = getDelay();
		String delayStr;
		if (delay == 0) {
			delayStr = "XXX";
		}
		else {
			delayStr = Float.toString((((Dijjer.PACKET_SIZE * 1000 / getDelay())) / 1024));
		}
		return delayStr + " k/sec, (w: "
				+ _simulatedWindowSize + ", r:" + _roundTripTime + ", d:"
				+ (((float) _droppedPackets / (float) _totalPackets)) + ")";
	}
}
